'use strict';

const Controller = require('egg').Controller;
const svgCaptcha = require('svg-captcha');
const fs = require('fs-extra');
class HomeController extends Controller {
  async index() {
    const { ctx } = this;
    ctx.body = 'hi, egg';
  }
  //TODO 图片验证码
  async svgImg(){
    const { ctx } = this;
    const captcha = svgCaptcha.create({
      size:4,
      fontSize:50,
      width:100,
      height:40,
      noise:3
    });
    ctx.session.captcha = captcha.text;
    ctx.response.type = "image/svg+xml";
    ctx.body = captcha.data;
  }
    //todo  文件上传
    async upload(){
      const { ctx } = this;
      console.log(ctx.request);
      const file = ctx.request.files[0];
      console.log(file);
      const {name} = ctx.request.body;
      console.log(name);
      // TODO 这是将前台传递的文件放到publice文件夹下
      await fes.move(file.filepath,this.config.UPLOAD__FILE+'/'+file.filename);
      ctx.body = '上传成功'
    }
}

module.exports = HomeController;
